/*
 *    Copyright 2022 CManLH
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.lifeonwalden.ms.service;

import com.lifeonwalden.ms.async.runner.service.AsyncRunner;
import com.lifeonwalden.ms.async.runner.service.RunnerService;
import com.lifeonwalden.ms.bean.Millis50Bean;
import com.lifeonwalden.ms.common.logger.LoggerUtil;
import com.lifeonwalden.ms.constant.RunnerServiceRegisterAnnounce;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Millis50RunnerSpringService implements RunnerService<Millis50Bean>, InitializingBean {
    private final static Logger logger = LoggerUtil.getLogger(Millis50RunnerSpringService.class);

    @Autowired
    private AsyncRunner asyncRunner;

    @Override
    public void run(Millis50Bean parameter) {
        logger.info("{} goto start", this.getClass().getName());

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        logger.info("{} goto end", this.getClass().getName());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.asyncRunner.registerRunner(RunnerServiceRegisterAnnounce.MILLIS_50, (p) -> this.run((Millis50Bean) p));
    }
}
